package com.phone.servlet;

import com.phone.ParameterCheck;
import com.phone.entity.Result;
import com.phone.exception.ErrorCode;
import com.phone.exception.ErrorException;
import com.phone.handle.AccountHandle;
import com.phone.handle.UserHandle;
import org.hibernate.Session;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;

/**
 * Created by tisen on 2017/9/22.
 */
@WebServlet(name = "Login", urlPatterns = "/login")
public class LoginServlet extends SuperServlet {
    private static final String QUICK_LOGIN = "quick";
    private static final String USER_LOGIN = "user";
    private static final String WX_LOGIN = "wx";
    private static final String QQ_LOGIN = "qq";
    private static final String PHONE = "phone";
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private static final String WX_OPENID = "wx_openid";
    private static final String QQ_OPENID = "qq_openid";

    @Override
    protected JSONObject Control(ParameterCheck check, Session session) throws Exception {
        JSONObject jsonObject = new JSONObject();

        String action = check.getParameterString(ACTION, false);
        switch (action) {
            case QUICK_LOGIN:
                jsonObject = quick_login(check, session);
                break;
            case USER_LOGIN:
                jsonObject = user_login(check, session);
                break;
            case WX_LOGIN:
                jsonObject = wx_login(check, session);
                break;
            case QQ_LOGIN:
                jsonObject = qq_login(check, session);
                break;
            default:
                throw new ErrorException(ErrorCode.UN_DEFINED_ACTION);
        }

        return jsonObject;
    }

    private JSONObject wx_login(ParameterCheck check, Session session) throws Exception {
        String wx = check.getParameterString(WX_OPENID, false);
        Result<String> result = new Result<>();
        result.setResult(SUCCESS);
        result.setReason("wx login successful");
        result.setObject(new AccountHandle(session).wx_login(wx));
        return new JSONObject(result);
    }

    private JSONObject qq_login(ParameterCheck check, Session session) throws Exception {
        String qq = check.getParameterString(QQ_OPENID, false);
        Result<String> result = new Result<>();
        result.setResult(SUCCESS);
        result.setReason("qq login successful");
        result.setObject(new AccountHandle(session).qq_login(qq));
        return new JSONObject(result);
    }

    private JSONObject user_login(ParameterCheck check, Session session) throws Exception {
        String username = check.getParameterString(USERNAME, false);
        String password = check.getParameterString(PASSWORD, false);
        Result<String> result = new Result<>();
        result.setResult(SUCCESS);
        result.setReason("user login successful");
        result.setObject(new AccountHandle(session).user_login(username, password));
        return new JSONObject(result);
    }

    private JSONObject quick_login(ParameterCheck check, Session session) throws Exception {
        String phone = check.getParameterString(PHONE, false);
        Result<String> result = new Result<>();
        result.setResult(SUCCESS);
        result.setReason("quick login successful");
        result.setObject(new AccountHandle(session).quick_login(phone));
        return new JSONObject(result);
    }

}
